package com.example.hrsystem.ui.task;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.data.model.TaskItem;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 任务列表适配器
 * 用于在RecyclerView中显示任务项
 */
public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.TaskViewHolder> {

    private List<TaskItem> taskList;
    private OnTaskItemClickListener listener;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());

    /**
     * 构造方法
     */
    public TaskListAdapter(List<TaskItem> taskList, OnTaskItemClickListener listener) {
        this.taskList = taskList;
        this.listener = listener;
    }

    /**
     * 创建ViewHolder
     */
    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.task_item_layout, parent, false);
        return new TaskViewHolder(itemView);
    }

    /**
     * 绑定ViewHolder
     */
    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        TaskItem task = taskList.get(position);
        holder.bind(task, listener);
    }

    /**
     * 获取项目数量
     */
    @Override
    public int getItemCount() {
        return taskList.size();
    }

    /**
     * 任务项ViewHolder
     */
    public static class TaskViewHolder extends RecyclerView.ViewHolder {

        private TextView titleTextView;
        private TextView contentTextView;
        private TextView taskTypeTextView;
        private TextView statusTextView;
        private TextView priorityTextView;
        private TextView createTimeTextView;
        private TextView deadlineTextView;
        private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());

        /**
         * 构造方法
         */
        public TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.task_item_title);
            contentTextView = itemView.findViewById(R.id.task_item_description);
            taskTypeTextView = itemView.findViewById(R.id.task_item_type);
            statusTextView = itemView.findViewById(R.id.task_item_status);
            priorityTextView = itemView.findViewById(R.id.task_item_priority);
            createTimeTextView = itemView.findViewById(R.id.task_item_create_time);
            deadlineTextView = itemView.findViewById(R.id.task_item_deadline);
        }

        /**
         * 绑定任务数据到视图
         */
        public void bind(final TaskItem task, final OnTaskItemClickListener listener) {
            titleTextView.setText(task.getTitle());
            contentTextView.setText(task.getContent());
            taskTypeTextView.setText(task.getTaskType());
            statusTextView.setText(task.getStatus());
            priorityTextView.setText(task.getPriority());

            // 设置状态显示颜色
            if ("未开始".equals(task.getStatus())) {
                statusTextView.setTextColor(itemView.getResources().getColor(R.color.colorWarning));
            } else if ("进行中".equals(task.getStatus())) {
                statusTextView.setTextColor(itemView.getResources().getColor(R.color.colorPrimary));
            } else if ("已完成".equals(task.getStatus())) {
                statusTextView.setTextColor(itemView.getResources().getColor(R.color.colorSuccess));
            }

            // 设置优先级显示颜色
            if ("高".equals(task.getPriority())) {
                priorityTextView.setTextColor(itemView.getResources().getColor(R.color.colorError));
            } else if ("中".equals(task.getPriority())) {
                priorityTextView.setTextColor(itemView.getResources().getColor(R.color.colorWarning));
            } else if ("低".equals(task.getPriority())) {
                priorityTextView.setTextColor(itemView.getResources().getColor(R.color.colorSuccess));
            }

            // 设置时间信息
            if (task.getCreateTime() != null) {
                createTimeTextView.setText("创建：" + dateFormat.format(task.getCreateTime()));
            }

            if (task.getDeadline() != null) {
                deadlineTextView.setText("截止：" + dateFormat.format(task.getDeadline()));
            }

            // 设置点击事件
            itemView.setOnClickListener(v -> listener.onTaskItemClick(task));
        }
    }

    /**
     * 任务项点击监听器接口
     */
    public interface OnTaskItemClickListener {
        void onTaskItemClick(TaskItem task);
    }
}